package com.ruoyi.finance.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.finance.mapper.VoucherTemplateDetailMapper;
import com.ruoyi.finance.mapper.VoucherTemplateTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.finance.domain.TVoucherTemplateDetail;
import com.ruoyi.finance.mapper.TVoucherTemplateMapper;
import com.ruoyi.finance.domain.TVoucherTemplate;
import com.ruoyi.finance.service.ITVoucherTemplateService;

/**
 * 凭证模板Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class TVoucherTemplateServiceImpl implements ITVoucherTemplateService 
{
    @Autowired
    private TVoucherTemplateMapper tVoucherTemplateMapper;

    @Autowired
    VoucherTemplateDetailMapper voucherTemplateDetailMapper;

    /**
     * 查询凭证模板
     * 
     * @param id 凭证模板主键
     * @return 凭证模板
     */
    @Override
    public TVoucherTemplate selectTVoucherTemplateById(Long id)
    {
        return tVoucherTemplateMapper.selectTVoucherTemplateById(id);
    }

    /**
     * 查询凭证模板列表
     * 
     * @param tVoucherTemplate 凭证模板
     * @return 凭证模板
     */
    @Override
    public List<TVoucherTemplate> selectTVoucherTemplateList(TVoucherTemplate tVoucherTemplate)
    {
        return tVoucherTemplateMapper.selectTVoucherTemplateList(tVoucherTemplate);
    }

    /**
     * 新增凭证模板
     * 
     * @param tVoucherTemplate 凭证模板
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTVoucherTemplate(TVoucherTemplate tVoucherTemplate)
    {
        TVoucherTemplate tVoucherTemplate1 =  tVoucherTemplateMapper.selectName(tVoucherTemplate.getName());
        if (StringUtils.isNotNull(tVoucherTemplate1)) {
            throw new RuntimeException("凭证模板名称已存在");
        }
        tVoucherTemplate.setCreateId(SecurityUtils.getUserId());
        tVoucherTemplate.setCreateTime(DateUtils.getNowDate());
        tVoucherTemplate.setDeletedType(1L);
        int rows = tVoucherTemplateMapper.insert(tVoucherTemplate);
        List<TVoucherTemplateDetail> tVoucherTemplateDetailList = tVoucherTemplate.getDetailList();
        if(tVoucherTemplateDetailList==null){
            throw new RuntimeException("请将信息添加完成");
        }else {
            for (TVoucherTemplateDetail tVoucherTemplateDetail : tVoucherTemplateDetailList)
            {
                TVoucherTemplateDetail vtd = new TVoucherTemplateDetail();
                vtd.setTemplateId(tVoucherTemplate.getId());
                vtd.setSubjectId(tVoucherTemplateDetail.getSubjectId());
                vtd.setAbstractId(tVoucherTemplateDetail.getAbstractId());
                vtd.setFxType(tVoucherTemplateDetail.getFxType());
                vtd.setDebitAmount(tVoucherTemplateDetail.getDebitAmount());
                vtd.setCreditAmount(tVoucherTemplateDetail.getCreditAmount());
                voucherTemplateDetailMapper.insert(vtd);
            }
        }
        return rows;
    }

    /**
     * 修改凭证模板
     * 
     * @param tVoucherTemplate 凭证模板
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTVoucherTemplate(TVoucherTemplate tVoucherTemplate)
    {
        tVoucherTemplate.setUpdateTime(DateUtils.getNowDate());
        tVoucherTemplateMapper.deleteTVoucherTemplateDetailByTemplateId(tVoucherTemplate.getId());
        insertTVoucherTemplateDetail(tVoucherTemplate);
        return tVoucherTemplateMapper.updateTVoucherTemplate(tVoucherTemplate);
    }

    /**
     * 批量删除凭证模板
     * 
     * @param ids 需要删除的凭证模板主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTVoucherTemplateByIds(Long[] ids)
    {
        tVoucherTemplateMapper.deleteTVoucherTemplateDetailByTemplateIds(ids);
        return tVoucherTemplateMapper.deleteTVoucherTemplateByIds(ids);
    }

    /**
     * 删除凭证模板信息
     * 
     * @param id 凭证模板主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTVoucherTemplateById(Long id)
    {
        tVoucherTemplateMapper.deleteTVoucherTemplateDetailByTemplateId(id);
        return tVoucherTemplateMapper.deleteTVoucherTemplateById(id);
    }

    /**
     * 新增凭证模板明细信息
     * 
     * @param tVoucherTemplate 凭证模板对象
     */
    public void insertTVoucherTemplateDetail(TVoucherTemplate tVoucherTemplate)
    {
        List<TVoucherTemplateDetail> tVoucherTemplateDetailList = tVoucherTemplate.getDetailList();
        Long id = tVoucherTemplate.getId();
        if (StringUtils.isNotNull(tVoucherTemplateDetailList))
        {
            List<TVoucherTemplateDetail> list = new ArrayList<TVoucherTemplateDetail>();
            for (TVoucherTemplateDetail tVoucherTemplateDetail : tVoucherTemplateDetailList)
            {
                tVoucherTemplateDetail.setTemplateId(id);
                list.add(tVoucherTemplateDetail);
            }
            if (list.size() > 0)
            {
                tVoucherTemplateMapper.batchTVoucherTemplateDetail(list);
            }
        }
    }
}
